const fs = require('fs');
const path = require('path');
const RSA = require('node-rsa');
// RSA解密
const keyPath = path.resolve(__dirname, './pem/rsa_private_key.pem');
const privatePem = fs.readFileSync(keyPath).toString();
const privateKey = new RSA(privatePem);
// 转成pkcs1格式
privateKey.setOptions({ encryptionScheme: 'pkcs1' });
module.exports = {
  /**
   * 解密
   * @param {*} value 需要解密的值 
   */
  decrypt(value) {
    let data = value;
    // 使用try...catch...捕获解密失败错误（如公钥错误），返回controller处理
    try {
      data = privateKey.decrypt(value, 'utf8');
      return data;
    } catch (err) {
      return false;
    }
  },
};